package com.hex.ds.hdtp.core.app.script.analyze.service.impl;

import com.hex.ds.hdtp.core.app.common.page.PageData;
import com.hex.ds.hdtp.core.app.common.page.converter.IPageAndPageDtoConverter;
import com.hex.ds.hdtp.core.app.common.page.dto.PageDto;
import com.hex.ds.hdtp.core.app.script.analyze.converter.IScriptAnalyzeListConverter;
import com.hex.ds.hdtp.core.app.script.analyze.dto.request.ScriptAnalyzeListRequestDto;
import com.hex.ds.hdtp.core.app.script.analyze.dto.response.ScriptAnalyzeListResponseDto;
import com.hex.ds.hdtp.core.app.script.analyze.service.IScriptAnalyzeListService;
import com.hex.ds.hdtp.core.app.script.analyze.service.IScriptAnalyzeTaskService;
import com.hex.ds.hdtp.core.inf.common.page.Page;
import com.hex.ds.hdtp.core.inf.script.analyze.po.ScriptAnalyzeListPo;
import com.hex.ds.hdtp.core.inf.script.analyze.repository.IScriptAnalyzeListRepository;
import lombok.RequiredArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.annotation.Lazy;
import org.springframework.stereotype.Service;

import java.util.List;

/**
 * @Package com.hex.ds.hdtp.core.app.script.analyze.service.impl
 * @ClassName ScriptAnalyzeListService
 * @Description 脚本分析清单-业务服务接口实现
 * @Author gj.xu
 * @Date 2023/12/8 14:14
 * @Version v2.0
 **/
@Slf4j
@Service
@RequiredArgsConstructor(onConstructor_ = {@Lazy, @Autowired})
public class ScriptAnalyzeListService implements IScriptAnalyzeListService {

    private final IScriptAnalyzeListConverter converter;

    private final IPageAndPageDtoConverter pageAndPageDtoConverter;

    private final IScriptAnalyzeListRepository scriptAnalyzeListRepository;

    private final IScriptAnalyzeTaskService scriptAnalyzeTaskService;

    /**
     * @Method queryListByPage
     * @Param
     * @Param scriptAnalyzeListRequestDto
     * @Param pageDto
     * @Return com.hex.ds.hdtp.core.app.common.page.PageData
     * @Description 分页查询
     * @Author gj.xu
     * @Date 2023/12/8 15:45
     * @Version V2.0
     */
    @Override
    public PageData queryListByPage(ScriptAnalyzeListRequestDto scriptAnalyzeListRequestDto, PageDto pageDto) {
        final Page<ScriptAnalyzeListPo> pageResult = scriptAnalyzeListRepository.queryListByPage(
                converter.toScriptAnalyzeListPo(scriptAnalyzeListRequestDto), pageAndPageDtoConverter.toPage(pageDto));
        List<ScriptAnalyzeListResponseDto> responseDtos = converter.toScriptAnalyzeListResponseDtoList(pageResult.getData());
        return new PageData(responseDtos, pageResult.getTotal());
    }

    /**
     * @Method queryByTaskId
     * @Param
     * @Param analyzeTaskPkId
     * @Return java.util.List<com.hex.ds.hdtp.core.app.script.analyze.dto.response.ScriptAnalyzeListResponseDto>
     * @Description 根据指定的分析任务主键查询清单
     * @Author gj.xu
     * @Date 2023/12/8 15:45
     * @Version V2.0
     */
    @Override
    public List<ScriptAnalyzeListResponseDto> queryByTaskId(String analyzeTaskPkId) {
        return converter.toScriptAnalyzeListResponseDtoList(scriptAnalyzeListRepository.queryListByTaskId(analyzeTaskPkId, null));
    }

    /**
     * @Method queryConsanguinityListByPage
     * @Param scriptAnalyzeListRequestDto
     * @Param pageDto
     * @Return com.hex.ds.hdtp.core.app.common.page.PageData
     * @Description 分页查询脚本分析的血缘
     * @Author gj.xu
     * @Date 2023/12/11 17:29
     * @Version V2.0
     */
    @Override
    public PageData queryConsanguinityListByPage(ScriptAnalyzeListRequestDto scriptAnalyzeListRequestDto, PageDto pageDto) {
        final Page<ScriptAnalyzeListPo> pageResult = scriptAnalyzeListRepository.queryConsanguinityListByPage(
                converter.toScriptAnalyzeListPo(scriptAnalyzeListRequestDto), pageAndPageDtoConverter.toPage(pageDto));
        List<ScriptAnalyzeListResponseDto> responseDtos = converter.toScriptAnalyzeListResponseDtoList(pageResult.getData());
        return new PageData(responseDtos, pageResult.getTotal());
    }

    /**
     * @Method add
     * @Param
     * @Param scriptAnalyzeListRequestDto
     * @Return void
     * @Description 新增
     * @Author gj.xu
     * @Date 2023/12/8 14:53
     * @Version V2.0
     */
    @Override
    public void add(ScriptAnalyzeListRequestDto scriptAnalyzeListRequestDto) {
        int count = scriptAnalyzeListRepository.add(converter.toScriptAnalyzeListPo(scriptAnalyzeListRequestDto));
        if (count != 1) {
            throw new RuntimeException("新增脚本清单失败，脚本名：" + scriptAnalyzeListRequestDto.getSourceFileName());
        }
    }

    /**
     * @Method modifyById
     * @Param
     * @Param scriptAnalyzeListRequestDto
     * @Return void
     * @Description 根据脚本转换清单主键更新
     * @Author gj.xu
     * @Date 2023/12/8 14:53
     * @Version V2.0
     */
    @Override
    public void modifyById(ScriptAnalyzeListRequestDto scriptAnalyzeListRequestDto) {
        int count = scriptAnalyzeListRepository.modifyById(converter.toScriptAnalyzeListPo(scriptAnalyzeListRequestDto));
        if (count != 1) {
            throw new RuntimeException("修改脚本清单失败，脚本名：" + scriptAnalyzeListRequestDto.getSourceFileName());
        }
    }

    /**
     * @Method removeById
     * @Param
     * @Param pkId
     * @Return void
     * @Description 删除脚本清单并更新任务总数
     * @Author gj.xu
     * @Date 2023/12/8 15:31
     * @Version V2.0
     */
    @Override
    public void removeById(String pkId) {
        ScriptAnalyzeListPo scriptAnalyzeListPo = scriptAnalyzeListRepository.queryById(pkId);
        // 删除对应脚本清单
        removeDataById(pkId);
        // 更新对应分析任务总数
        updateScriptAnalyzeTask(scriptAnalyzeListPo);
    }

    private void removeDataById(String pkId) {
        int count = scriptAnalyzeListRepository.removeById(pkId);
        if (count != 1) {
            throw new RuntimeException("删除脚本清单失败");
        }
    }

    /**
     * @Method updateScriptAnalyzeTask
     * @Param
     * @Param scriptAnalyzeListPo
     * @Return void
     * @Description 更新对应分析任务总数
     * @Author gj.xu
     * @Date 2023/12/8 15:31
     * @Version V2.0
     */
    private void updateScriptAnalyzeTask(ScriptAnalyzeListPo scriptAnalyzeListPo) {
        Integer total = queryByTaskId(scriptAnalyzeListPo.getAnalyzeTaskPkId()).size();
        scriptAnalyzeTaskService.modifyTotal(scriptAnalyzeListPo.getAnalyzeTaskPkId(), total);
    }


    /**
     * @Method removeByTaskId
     * @Param analyzeTaskPkId
     * @Return void
     * @Description 根据分析任务主键删除
     * @Author gj.xu
     * @Date 2023/12/11 17:25
     * @Version V2.0
     */
    @Override
    public void removeByTaskId(String analyzeTaskPkId) {
        scriptAnalyzeListRepository.removeByTaskId(analyzeTaskPkId);
    }

    @Override
    public void modifyStatus(String pkId, String status, String log) {
        scriptAnalyzeListRepository.modifyStatus(pkId, status, log);
    }

    @Override
    public void modifyStartTsById(String pkId) {
        scriptAnalyzeListRepository.modifyStartTsById(pkId);
    }

    @Override
    public void modifyEndTsById(String pkId) {
        scriptAnalyzeListRepository.modifyEndTsById(pkId);
    }
}
